﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using cbmobile.DAC;
using System.Data;
using PQT.API;
using cbmobile.BLC;

namespace cbmobile.BLC
{
    public class BLC_Member
    {
        private CBMobileEntities da = null;
        public BLC_Member()
        {
            da = new CBMobileEntities();
        }

        public IList<Member> All_Member()
        {
            return da.Members.ToList();
        }

        public Member Get_Member_By_ID(int Member_ID)
        {
            IList<Member> list = da.Members.Where(p => p.UserID == Member_ID).ToList();
            if (list.Count() == 0)
                return null;
            return list.First();
        }

        public bool Get_Member_By_LoginName(string name)
        {
            IList<Member> list = da.Members.Where(p => p.TenDangNhap == name).ToList();
            if (list.Count() == 0)
                return true;
            return false;
        }

        public int Last_Member()
        {
            IList<Member> list = da.Members.ToList();
            int n = list.Last().UserID;
            return n;
        }
        public IList<Member> Rows_Member(int p_page, int p_pageSize, string p_name)
        {
            int preCount = (p_page - 1) * p_pageSize;
            IList<Member> list = da.Members.Where(z => z.TenDangNhap.Contains(p_name)).OrderByDescending(z => z.TenDangNhap).Skip(preCount).Take(preCount + p_pageSize).ToList();
            return list;
        }


        public int GetTotalRecord(string p_name)
        {
            return da.Members.Where(z => z.TenDangNhap.Contains(p_name)).Count();
        }

        public void Delete(int Member_ID)
        {
            var existing = da.Members.Where(p => p.UserID == Member_ID).First();
            existing.TrangThai = (int)CommonStatus.Delete;
            da.SaveChanges();
        }

        #region change password
        public void ChangePassword(int User_ID, string NewPass)
        {
            var objEnt = da.Members.Where(p => p.UserID == User_ID).First();

            objEnt.MatKhau = NewPass;
            da.SaveChanges();
        }
        #endregion


        #region update
        public void Update(int UserID,
                           string TenDangNhap,
                           string TenDayDu,
                           DateTime NgaySinh,
                           bool GioiTinh,
                           string Email,
                           string DiaChi,
                           string SoDienThoai,
                           int TrangThai)
        {
            var objEnt = da.Members.Where(p => p.UserID == UserID).First();

            objEnt.TenDangNhap = TenDangNhap;
            objEnt.TenDayDu = TenDayDu;
            objEnt.NgaySinh = NgaySinh;
            objEnt.GioiTinh = GioiTinh;
            objEnt.Email = Email;
            objEnt.DiaChi = DiaChi;
            objEnt.SoDienThoai = SoDienThoai;
            objEnt.TrangThai = TrangThai;

            da.SaveChanges();
        }
        #endregion

        #region Insert
        public bool Insert(string Tendangnhap,
                           string TenDayDu,
                           DateTime NgaySinh,
                           bool GioiTinh,
                           string Email,
                           string MatKhau,
                           string DiaChi,
                           string SoDienThoai,
                           int TrangThai)
        {
            bool ss = false;
            IList<Member> list = da.Members.Where(p => p.TenDangNhap==Tendangnhap).ToList();
            if (list.Count() == 0)
            {
                var objEnt = new Member();
                objEnt.TenDangNhap = Tendangnhap;
                objEnt.TenDayDu = TenDayDu;
                objEnt.NgaySinh = NgaySinh;
                objEnt.GioiTinh = GioiTinh;
                objEnt.Email = Email;
                objEnt.MatKhau = MatKhau;
                objEnt.DiaChi = DiaChi;
                objEnt.SoDienThoai = SoDienThoai;
                objEnt.TrangThai = TrangThai;
                da.Members.AddObject(objEnt);
                da.SaveChanges();
                ss = true;
            }
            return ss;
        }
        #endregion
    }
}